<template>

	<view class="page">

		<uni-card margin='2px' style="margin-bottom: 20px; padding: 5px;">
			<view @click="openSelector">
			<mpopupHeaderItem label="经营者名称" :value="order.operatorName  || '[修改]'" height="90px" />
			<mpopupHeaderItem label="经营地址" :value="order.address  || '[修改]'" height="90px" />
			<mpopupHeaderItem label="受理号" :value="order.acceptNumber || '[修改]'" height="90px" />

			<mpopupHeaderRadioItem label="申请类型" height="90px" :custom-class="'custom-align'" :show-bottom-border="true"
				:custom-style="{ padding: '20rpx' }">
				<text>{{bidTypeText}} </text>
			</mpopupHeaderRadioItem>
			</view>
		</uni-card>

		<uni-card margin='2px' style="margin-bottom: 20px; padding: 5px;">
			<uni-title type="h1" align="center" title="撤销理由"></uni-title>
			<view class="uni-column">
				<input v-model="order.remark" class="uni-input" placeholder="请填写相关内容" />
			</view>
		</uni-card>

		<view class="button-container" v-show="editStatus!='FALSE'">
			<button style="margin-bottom: 20px;" type="primary" @click="submitForm('1')">保存并提交撤销审查单</button>
			<button type="primary" @click="submitForm('0')">暂存</button>
		</view>
		
		<EnterpriseSelector tableType="1" :query="{orderType: '1', auditStatus: '1'}" type="radio"
			ref="selector" :multiple="false" @confirm="handleConfirm" @close="handleClose" />
		
	</view>

</template>

<script>
	import mpopupHeaderItem from "@/components/mpopup-header-item.vue"
	import mpopupHeaderRadioItem from "@/components/mpopup-header-radio-item.vue"
	import EnterpriseSelector from "@/components/dialog/enterprise-dialog.vue"
	import {
		addCategory,
		categoryInfo,
		getMedicalDict,
		addReviewReport,
		categoryUpdate
	} from '@/api/order'
	export default {
		onLoad(options) {
			
			if (options.editStatus != null) {
				const editStatus = decodeURIComponent(options.editStatus);
				const workOrderId = decodeURIComponent(options.workOrderId);
				this.workOrderId=workOrderId;
				this.getcategoryInfo(workOrderId);
				this.editStatus = options.editStatus;
			} else {
				//上个界面的传参
				if (options.orderData) {
					const bindType = decodeURIComponent(options.bindType);
					const orderData = JSON.parse(decodeURIComponent(options.orderData));
					this.workOrderId=orderData.workOrderId;
					this.order = orderData;
		
				}
			}
			this.getMedicalDict('bid_type')
		},
		components: {
			mpopupHeaderRadioItem,
			mpopupHeaderItem, // 需先导入组件
EnterpriseSelector
		},
		computed: {
			bidTypeText() {
				const found = this.valueOptions.find(
					item => item.value == String(this.order.bidType) // 统一转换为字符串比较
				);
				return found ? found.text : this.order.bidType;
			}
		},
		data() {
			return {
				order: {
					audit_status: '0',
					operatorName: '',
					acceptNumber: '',
					address: '',
					bidType: '',
					remark: ''
				},
				workOrderId:'',
				editStatus: 'ADD',
				valueOptions: [],
			}
		},
		methods: {
			// 打开选择器
			openSelector() {
				this.$refs.selector.open()
			},
			handleConfirm(selected) {
				this.order = selected
			},
			handleClose() {
				console.log('弹窗已关闭')
			},
		/// 提交方法
		async submitForm(status) {
			try {
				this.order.audit_status = status;
				this.order.workOrderId=this.workOrderId
				
				// 状态校验前置
				if (this.editStatus === 'FALSE') {
					return uni.showToast({
						title: '当前处于查看状态',
						icon: 'none'
					});
				}
				// 统一处理提交逻辑
				const isEditMode = this.editStatus === 'EDIT';
				const apiMethod = isEditMode ? categoryUpdate : addCategory;
				// 构造请求参数（避免污染原始数据）
				const payload = {
					...this.order,
					auditStatus: status,
				};
		
				// 显示加载状态
				uni.showLoading({
					title: '提交中...',
					mask: true
				});
		
				// 执行API调用
				const res = await apiMethod(payload);
		
				if (res.code !== 0) {
					throw new Error(res.msg || '服务器处理异常');
				}
		
				// 统一处理成功逻辑
				uni.showToast({
					title: '提交成功',
					icon: 'success'
				});
		
		// 关闭当前页面，跳转到工单页
		this.$closeAndRedirect({
		  url: '/pages/work/history/index'
		});
		
			} catch (error) {
				// 统一错误处理
				console.error('提交失败:', error);
				uni.showToast({
					title: error.msg || '请求失败，请检查网络',
					icon: 'none',
					duration: 3000
				});
			} finally {
				uni.hideLoading();
			}
		},
			//获取字典值
			async getMedicalDict(type) {
				try {
					const res = await getMedicalDict(type)
					// 增加响应状态判断
					if (res.code === 0) {
						if (type === 'bid_type') {
							this.valueOptions = res.data.map(item => ({
								text: item.label,
								value: String(item.value) // 确保字典值转为字符串
							}));
console.log(this.valueOptions)
						}
					} else {
						throw new Error(res?.data?.msg || '接口返回异常')
					}
				} catch (error) {
					uni.showToast({
						title: error.message || '获取失败',
						icon: 'none',
						duration: 2000
					})
				}
			},
			// 获取工单详情
			async getcategoryInfo(orderId) {

				const res = await categoryInfo(orderId);
				if (res.code === 0) {
					this.order = res.data;
					// 确保先获取工单数据再获取字典
					await this.getMedicalDict('bid_type');
				}
			},

		}
	}
</script>

<style>
	/* 网页容器样式 */
	.page {
		flex: 1;
		background-color: #e9f1fe;
		padding: 20px;
		height: 100%;
	}


	/* 按钮容器样式 */
	.button-container {
		left: 0;
		right: 0;
		padding: 15px 20px;
		background: #ffffff;
		border-radius: 4px;
		box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);

	}


	.uni-column {
		padding: 20px;
		background-color: #e9f1fe;
		height: 300px;
		margin-top: 10px;
		margin-bottom: 10%;
		border-radius: 10px;
	}
</style>